package com.employeemanagementsystem.controller;

import com.employeemanagementsystem.dao.UserDao;
import com.employeemanagementsystem.model.User;

import jakarta.servlet.http.HttpSession;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/update_user_info")
public class UserInfoUpdateServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user != null) {
            String fullName = request.getParameter("fullName");
            String email = request.getParameter("email");
            String department = request.getParameter("department");
            String position = request.getParameter("position");

            user.setFullName(fullName);
            user.setEmail(email);
            user.setDepartment(department);
            user.setPosition(position);

            UserDao userDao = new UserDao();
            if (userDao.updateUser(user)) {
                response.sendRedirect("employee_home.jsp?message=User information updated successfully!");
            } else {
                response.sendRedirect("user_info_update.jsp?error=Failed to update user information.");
            }
        } else {
            response.sendRedirect("login.jsp?error=Please log in first.");
        }
    }
}



